package com.jr.controller;

import com.jr.entry.BlogText;
import com.jr.pojo.ShowBlog;
import com.jr.service.BlogTextService;
import com.jr.util.Page;
import com.jr.util.ResultObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController /*替换@RestController=@Controller + @ResponseBody*/
@RequestMapping("/blog/")
public class BlogTextController {
    @Autowired
    private BlogTextService blogTextService;
    @Autowired
    private ResultObject resultObject;

    @RequestMapping("showBy")
    public ResultObject showBy(BlogText blogText, Integer indexPage){
        Page page = new Page();
        ResultObject rs = new ResultObject();
        if (indexPage != null){
            page.setIndexPage(indexPage);
        }
        int size = blogTextService.showBlogTextSize(blogText);
        page.setCountData(size);
        page.setCountPage(size / 3 + 1);
        RowBounds rb = new RowBounds(3 * (page.getIndexPage() - 1), 3);

        rs.setStatus(100);
        rs.setResult(blogTextService.showBlogTextBy(blogText, rb));
        rs.setPage(page);
        return rs;
    }

    @RequestMapping("showByStatus")
    public ResultObject showByStatus(Integer status){
        System.out.println(status);
        ArrayList<BlogText> blogTexts = blogTextService.showByStatus(status);
        ResultObject rs = new ResultObject();
        rs.setStatus(100);
        rs.setResult(blogTexts);
        return rs;
    }

    @RequestMapping("showText")
    public ResultObject showText(Integer blogid){
        resultObject.setStatus(100);
        resultObject.setResult(blogTextService.showTextByBlogId(blogid));
        return resultObject;
    }

    @RequestMapping("showByDesc")
    public ResultObject showByDesc(){
        ResultObject rs = new ResultObject();
        rs.setStatus(100);
        rs.setResult(blogTextService.showBlogTextByClickNumber());
        return rs;
    }

    @RequestMapping("findByUid")
    public ResultObject findByUid(BlogText blogText){
        resultObject.setStatus(400);
        ArrayList<ShowBlog> blogTexts = blogTextService.showBackground(blogText);
        resultObject.setResult(blogTexts);
        return resultObject;
    }

    @RequestMapping("findByblogid")
    public ResultObject findByblogid(BlogText blogText){
        resultObject.setStatus(400);
        ArrayList<ShowBlog> blogTexts = blogTextService.showBackground1(blogText);
        resultObject.setResult(blogTexts);
        return resultObject;
    }

    @RequestMapping("updateByBlogId")
    public ResultObject updateByBlogId(BlogText blogText){
        boolean b = blogTextService.editBlogText(blogText);
        if (b) {
            resultObject.setStatus(400);
            resultObject.setResult("修改成功");
        }else {
            resultObject.setStatus(406);
            resultObject.setResult("修改失败");
        }
        return resultObject;
    }

    @RequestMapping("deleteByBlogId")
    public ResultObject deleteByBlogId(BlogText blogText){
        boolean b = blogTextService.removeBlogText(blogText);
        if (b) {
            resultObject.setStatus(400);
            //resultObject.setResult("删除成功");
            ArrayList<ShowBlog> blogTexts = blogTextService.showBackground(blogText);
            resultObject.setResult(blogTexts);
        }else {
            resultObject.setStatus(406);
            resultObject.setResult("删除失败");
        }
        return resultObject;
    }

    @RequestMapping("addByBlogId")
    public ResultObject addByBlogId(BlogText blogText){
        boolean b = blogTextService.addBlogText(blogText);
        System.out.println(blogText);
        if (b) {
            resultObject.setStatus(400);
            resultObject.setResult("新增成功");
        }else {
            resultObject.setStatus(406);
            resultObject.setResult("新增失败");
        }
        return resultObject;
    }
    
}
